import 'package:flutter/widgets.dart';
import 'package:mvvm_provider/base/base_state.dart';
import 'package:mvvm_provider/base/base_view_model.dart';
import 'package:mvvm_provider/base/multi_state_widget.dart';
import 'package:provider/provider.dart';

enum PlaceHolderType {
  /// ListView站位
  listViewPlaceHolder,

  /// GridView站位
  gridViewPlaceHolder,

  /// StaggeredGrid 站位
  staggeredGridPlaceHolder,

  /// 详情 站位
  detailPlaceHolder,

  /// 无骨架屏展示loading
  noPlaceHolder,
}

class ConsumerStatusWidget<T extends BaseViewModel<S>, S extends BaseState>
    extends StatelessWidget {
  final Widget? emptyWidget;
  final Widget? errorWidget;
  final String? emptyText;
  final String? errorText;
  final String? timeOutText;
  final Function? refreshMethod;
  final PlaceHolderType placeHolderType;
  final Widget Function(BuildContext context, S state, Widget? child) builder;
  final Widget? child;

  const ConsumerStatusWidget(
      {super.key,
      required this.builder,
      this.child,
      this.emptyWidget,
      this.errorWidget,
      this.emptyText,
      this.errorText,
      this.timeOutText,
      this.refreshMethod,
      required this.placeHolderType});

  @override
  Widget build(BuildContext context) {
    return Consumer<T>(
      builder: (context, viewModel, child) {
        return MultiStateWidget(
          netState: viewModel.state.netState,
          placeHolderType: placeHolderType,
          emptyWidget: emptyWidget,
          errorWidget: errorWidget,
          timeOutText: timeOutText,
          emptyText: emptyText,
          errorText: errorText,
          refreshMethod: refreshMethod,
          builder: (BuildContext context) {
            return builder(context, viewModel.state, child);
          },
        );
      },
      child: child,
    );
  }
}
